<?php
/**
 * 轮播管理
 */
namespace Admin\Controller;
use Admin\BaseController;
use Common\Model\MarqueeModel;
use Think\Page;
class MarqueeController extends BaseController{

	public function baseMarquee($position){
		$size=MarqueeModel::getSize($position);
		$list=MarqueeModel::getMarqueeList($position,100);
		$maxOrder = 0;
		foreach ($list as $val){
			if($val['showOrder'] > $maxOrder){
				$maxOrder = $val['showOrder'];
			}
		}
		$maxOrder +=1;
		$this->smarty->assign('maxOrder',$maxOrder);
		$this->smarty->assign('picWidth',$size[0]);
		$this->smarty->assign('picHeight',$size[1]);
		$objType=C('OBJ_TYPE');
		unset($objType[0]);
		$this->smarty->assign('objType',$objType);
		$this->smarty->assign('list',$list);
		$this->smarty->assign('position',$position);
		$this->smarty->assign('OUTER',OBJ_TYPE_OUTER);

		$this->smarty->display('Marquee/marquee.html');
	}
	/**
	 * 商品列表页
	 * Author: shijy
	 */
	public function indexMarquee(){
		$this->baseMarquee(MarqueeModel::MARQUEE_WAP_INDEX);
	}

	public function webIndexMarquee(){
		$this->baseMarquee(MarqueeModel::MARQUEE_WEB_INDEX);

	}
	public function pointMarquee(){
		$this->baseMarquee(MarqueeModel::MARQUEE_WAP_POINT_MALL);
		
	}

	public function indexDelete(){
		$returnData=array(
			'status'=>0,
			'msg'=>''
		);
		$id=I('post.id',0,'int');
		if($id<1){
			$returnData['status']=1;
			$returnData['msg']='请指定对象';
			$this->ajaxReturn($returnData);
		}
		M("marquee",null,DB_MAIN_CFG)->where("id={$id}")->delete();
		$this->ajaxReturn($returnData);
	}

	//资讯添加或修改
	public function indexSave(){
		$id=I('post.id',0,'int');
		$title=I('post.title','');
		$title=htmlspecialchars_decode($title);
		$position=I('post.position',0,'int');

		$relatedType=I('post.relatedType',0,'int');
		$relatedID=I('post.relatedID',0,'int');//
		$pic=I('post.pic');//内容
		$showOrder=I('post.showOrder',0,'int');
		$isOn = I('post.isOn',0,'intval');	//上架状态


		$data=array(
			'jumpUrl'		=>$title,
			'pic'			=>$pic,
			'relatedType'	=>$relatedType,
			'relatedID'		=>$relatedID,
			'showOrder'		=>$showOrder,
			'position'		=>$position,
			'isOn'			=>$isOn,
		);

		$marqueeM = M("marquee",null,DB_MAIN_CFG);

		$returnData=array(
			'status'=>0,
			'msg'=>''
		);
		if($id==0){//新增
			$data['addtime'] = time();
			$result=$marqueeM->add($data);
			if($result==false){
				$returnData['status']=1;
				$returnData['msg']="添加失败";
				$this->ajaxReturn($returnData);
			}
			$returnData['data']=$result;
			$returnData['msg']="添加成功";
			$this->ajaxReturn($returnData);

		}else{//修改
			$marqueeM->where("id={$id}")->save($data);
			$returnData['msg']="修改成功";
			$this->ajaxReturn($returnData);
		}
	}
	//轮播图上架或下架
	public function doOnOff(){
		$id = I('post.id',0,'intval');
		$isOn = I('post.isOn',0,'intval');
		if(!$id){
			$this->ajaxReturn(array('status' =>1,'msg' => '参数错误'));
		}
		$marqueeM = M("marquee",null,DB_MAIN_CFG);
		if($isOn==0){
			$marqueeM->where("id={$id}")->setField('isOn',1);
			$this->ajaxReturn(array('status'=>0,'msg'=>'上架成功'));
		} else{
			$marqueeM->where("id={$id}")->setField('isOn',0);
			$this->ajaxReturn(array('status'=>0,'msg'=>'下架成功'));
		}
	}



}
